<template>
  <div>
    <input type="text" v-model="link" />
    <div>
      <a :href="link">{{ link }}</a>
    </div>

    <van-button @click="postMsg">postMsg</van-button>
    <button @click="skip('navigateTo')">navigateTo</button>
    <button @click="skip('redirectTo')">redirectTo</button>
    <button @click="skip('navigateBack')">navigateBack</button>
    <button @click="skip('reLaunch')">reLaunch</button>
  </div>
</template>
<script>
import { on, off } from "@/utils/dom";
export default {
  data() {
    return {
      link: "",
    };
  },
  mounted() {
    if (this.$uni.env.miniprogram) {
      console.log(this.$route.query);
    }
    on(document, "visibilitychange", this.visibilitychange);
    // 记得销毁一些全局的的事件
    this.$once("hook:beforeDestroy", () => {
      off(document, "visibilitychange", this.visibilitychange);
    });
  },
  methods: {
    visibilitychange() {
      if (document.visibilityState == "visible") {
        // document.title = "页面可见";
        console.log("页面可见");
        //do something
      }
      if (document.visibilityState == "hidden") {
        // document.title = "页面不可见";
        console.log("页面不可见");
        //do something else
      }
    },
    postMsg() {
      console.log("env", this.$uni.env);
    },
    skip(action) {
      switch (action) {
        case "reLaunch":
          this.$uni.reLaunch({
            url: "/pages/ad/ad",
          });
          break;
        case "navigateBack":
          this.$uni.navigateBack({
            delta: 1,
          });
          break;
        default:
          this.$uni[action]({
            url: "/pages/pay/pay",
          });
          break;
      }
    },
  },
};
</script>
